var util = require("../../utils/util.js");
var api = require("../../config/api.js");


Page({
    /**
     * 页面的初始数据
     */
    data: {
        disabled:false,
        adultCount:0,
        childCount:0,
        adultList:[],
        childList:[],
        adultPrice:0,
        childPrice:0,
        adultTotal:0,
        childTotal:0,
        total:0,
        passStatus:true,
    },
    onChangeSteper(event) {
        //console.log(event.detail);
        let adultTotal = event.detail*this.data.adultPrice
        let childTotal = this.data.childCount*this.data.childPrice
        let total = adultTotal+childTotal
        this.setData({
            adultCount:event.detail,
            adultTotal:adultTotal.toFixed(2),
            childTotal:childTotal.toFixed(2),
            total:total.toFixed(2)
        })
        
    },
    onChangeSteper1(event) {
        //console.log(event.detail);
        let adultTotal = this.data.adultCount*this.data.adultPrice
        let childTotal = event.detail*this.data.childPrice
        let total = adultTotal+childTotal
        this.setData({
            childCount:event.detail,
            adultTotal:adultTotal.toFixed(2),
            childTotal:childTotal.toFixed(2),
            total:total.toFixed(2)
        })
    },
    onAdultPlus(){
        let adultList = this.data.adultList.concat([{
            isAdult:true,
            name:'',
            phone:null,
            id:null
        }])
        this.setData({
            adultList:adultList
        })
    },
    onAdultMinus(){
        let ind = this.data.adultList.length-1
        //console.log(ind)
        let adultList = this.data.adultList.splice(ind,1);
        this.setData({
            adultList:adultList
        })
        if(this.data.adultCount == 0){
            this.setData({
                adultList:[]
            })
        }
    },
    onChildPlus(){
        let childList = this.data.childList.concat([{
            isAdult:false,
            name:'',
            phone:null,
            id:null
        }])
        this.setData({
            childList:childList
        })
    },
    onChildMinus(){
        let ind = this.data.childList.length-1
        let childList = this.data.childList.splice(ind,1);
        this.setData({
            childList:childList
        })
        if(this.data.childCount == 0){
            this.setData({
                childList:[]
            })
        }
    },
    //输入的信息
    formInfos: function (e) {
        const that = this;
        let name = e.currentTarget.dataset.name;
        let index = e.currentTarget.dataset.index;
        let list = e.currentTarget.dataset.list;
        let data = list+"["+index+"]."+name;
        let reg = e.currentTarget.dataset.reg
        //console.log(data)
        // console.log(e.detail)
        that.setData({
            [data]: e.detail.value
        });
    },
    // onBlur(e){
    //     const that = this;
    //     let val = e.detail.value;
    //     let title = e.currentTarget.dataset.title;
    //     let regMoblie = /^1[3456789]\d{9}$/;
    //     let regId = /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/;
    //     let regs;
    //     if(title=='手机号'){
    //         regs = regMoblie
    //     } else if(title=='身份证号码'){
    //         regs = regId
    //     }
    //     if(val === ''){
    //         wx.showToast({
    //             icon:'none',
    //             title: title+'不能为空',
    //         })
    //         this.setData({
    //             passStatus:false
    //         })
    //         return
    //     }
    //     if(val != '' && title != '姓名'){
    //         console.log(regs,val)
    //         if(!regs.test(val)){
    //             wx.showToast({
    //                 icon:'none',
    //                 title: title+'格式不正确',
    //             })
    //             this.setData({
    //                 passStatus:false
    //             })
    //             return
    //         }
    //     }
    //     this.setData({
    //         passStatus:true
    //     })
    // },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let adultTotal = options.adultPrice
        let childTotal = options.childPrice
        let total = Number(adultTotal)+Number(childTotal)
        this.setData({
            merchantCommodityId:options.merchantCommodityId,
            sourceId:options.sourceId,
            adultPrice:options.adultPrice,
            childPrice:options.childPrice
        })
    },
    submitInfos(){
        var that = this
        let list = this.data.adultList.concat(this.data.childList);
        var regMoblie = /^1[3456789]\d{9}$/;
        var regId = /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/;
        this.setData({
            passStatus:true
        })
        //console.log(list)
        if(list.length == 0){
            wx.showToast({
                icon:'none',
                title: '请填写资料',
            })
            this.setData({
                passStatus:false
            })
            return
        }
        for(var i = 0 ; i < list.length; i++){
        　　if(list[i].name == '' || list[i].name == null){
                wx.showToast({
                    icon:'none',
                    title: '请输入姓名',
                })
                this.setData({
                    passStatus:false
                })
        　　　　 break; 
            }
            else if(list[i].phone == '' || list[i].phone == null){
                wx.showToast({
                    icon:'none',
                    title: '请输入手机号',
                })
                this.setData({
                    passStatus:false
                })
        　　　　 break; 
            }
            if(!regMoblie.test(list[i].phone)){
                wx.showToast({
                    icon:'none',
                    title: '手机号格式错误',
                })
                this.setData({
                    passStatus:false
                })
                break;
            }
            else if(list[i].id == '' || list[i].id == null){
                wx.showToast({
                    icon:'none',
                    title: '请输入身份证号',
                })
                this.setData({
                    passStatus:false
                })
        　　　　 break; 
            }

            else if(!regId.test(list[i].id)){
                wx.showToast({
                    icon:'none',
                    title: '身份证号格式错误',
                })
                this.setData({
                    passStatus:false
                })
                break;
            }
        }
        if(that.data.passStatus){
            let subData={}
            subData.childCount=this.data.childCount
            subData.adultCount=this.data.adultCount
            subData.list=list
            //console.log(subData)
            let str = JSON.stringify(subData)
            that.onBuy(str)
        }
        
    },
    onBuy(item){
        let _this = this;
        wx.showLoading({
            title: '购买中...',
          })
        this.setData({
            disabled:true
        })
        util.request(api.surroundings.userBuy,{
            sourceId:this.data.sourceId,
            item:item,
            merchantCommodityId:this.data.merchantCommodityId
        },'post').then(res=>{
            wx.requestPayment({
                'timeStamp': res.result.timeStamp,
                'nonceStr': res.result.nonceStr,
                'package': res.result.packageValue,
                'signType': res.result.signType,
                'paySign': res.result.paySign,
                'success': function (res) {
                    _this.setData({
                        disabled:false
                    })
                    wx.hideLoading()
                    //订阅消息
                    let templIds = ['qUgT6qfdGEhZwW7iHPcwzFAUq9EElyS3oP_8EzMlINE']
                    wx.requestSubscribeMessage({
                        tmplIds: templIds,
                        success (res) {
                            console.log(res)
                            wx.navigateTo({
                                url: '/Surroundings/orderResult/orderResult',
                            })
                        },
                        fail (err) {
                            console.log(err)
                            wx.navigateTo({
                                url: '/Surroundings/orderResult/orderResult',
                            })
                        }
                    })
                },
                'fail': function (res) {
                    wx.hideLoading()
                    wx.showToast({
                        title: "支付失败",
                        icon: "none"
                    })
                    _this.setData({
                        disabled:false
                    })
                }
            });
        }).catch(err=>{
            console.log(err)
            wx.showToast({
              title: '购买失败，请重新购买～',
            })
            wx.hideLoading()
            this.setData({
                disabled:false
            })
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {},

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {},

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
        
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {},

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {},

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {},
    
});
